// pages/shopcar/shopcar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    Total:0,
    shopcarlist:[
      {
      shopname:'秦巴中源',
      checked:false,
      list:[
        { img: '../../resource/image/cp1.jpg', name: '【自营店】泸州老窖• 茗酿40.8°  巴中有货 收货地址选巴中 ', zt: '缺货', price: 199.99, gg: '500ml', num: 1, checked: false, subtotal: 199.99},
        { img: '../../resource/image/cp2.jpg', name: '【自营店】泸州老窖• 茗酿40.8°  巴中有货 收货地址选巴中 ', zt: '有货', price: 9.99, gg: '500ml', num: 1, checked: false, subtotal: 9.99}
      ]
      },
      {
        shopname: '秦巴中源', 
        checked: false,
        list: [
          { img: '../../resource/image/cp7.jpg', name: '【自营店】泸州老窖• 茗酿40.8°  巴中有货 收货地址选巴中 ', zt: '缺货', price: 199.99, gg: '500ml', num: 1, checked: false, subtotal: 199.99 }
        ]
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    rudc:function(e){
      var itemindex = e.currentTarget.dataset.itemindex;
      var listindex = e.currentTarget.dataset.listindex;
      let carts = this.data.shopcarlist;
      let num = carts[itemindex].list[listindex].num
      let price = carts[itemindex].list[listindex].price
      let subtotal = carts[itemindex].list[listindex].subtotal
      if(num == 1){
        num == 1;
        subtotal = num * price
      }else{
        num = num - 1;
        subtotal = num * price
      }
      carts[itemindex].list[listindex].num = num;
      carts[itemindex].list[listindex].subtotal = subtotal;
      this.setData({ shopcarlist: carts })
      this.jszj()
    },
    add: function (e) {
      var itemindex = e.currentTarget.dataset.itemindex;
      var listindex = e.currentTarget.dataset.listindex;
      let carts = this.data.shopcarlist;
      let num = carts[itemindex].list[listindex].num
      let price = carts[itemindex].list[listindex].price
      let subtotal = carts[itemindex].list[listindex].subtotal
      num = num + 1;
      subtotal = num * price

      carts[itemindex].list[listindex].num = num;
      carts[itemindex].list[listindex].subtotal = subtotal;
      this.setData({ shopcarlist: carts })
      this.jszj()
    },
    checkboxChange:function(e){
      let carts = this.data.shopcarlist;
      let dqindex = e.currentTarget.dataset.index
      let list = carts[dqindex].list
      carts[dqindex].checked = !carts[dqindex].checked
      this.setData({ shopcarlist: carts })
      if (this.data.shopcarlist[dqindex].checked){
        list.forEach((item,index)=>{
          item.checked = true
        })
        this.setData({ shopcarlist: carts })
      }else{
        list.forEach((item, index) => {
          item.checked = false
        })
        this.setData({ shopcarlist: carts })
      }
      this.jszj()
    },
    listCheckChange:function(e){
      let carts = this.data.shopcarlist;
      let fjindex = e.currentTarget.dataset.fjindex
      let dqindex = e.currentTarget.dataset.index
      let list = carts[fjindex].list
      var length = 0;
      
      carts[fjindex].list[dqindex].checked = !carts[fjindex].list[dqindex].checked;
      list.forEach((item, index) => {
        if (item.checked==true){
          length = length+1;
        }
      })
      if (length == list.length){
        carts[fjindex].checked = true
      }else{
        carts[fjindex].checked = false
      }
      this.setData({ shopcarlist: carts })
      this.jszj()
    },
    jszj:function(){
      let carts = this.data.shopcarlist;
      // carts.forEach((item,index)=>{
      //   item.forEach((it)=>{
      //     console.log(it)
      //   })
      // })
      var zjg = 0;
      for (var i = 0; i < carts.length; i++){
        for (var y = 0; y < carts[i].list.length;y++){
          // console.log(carts[i].list[y])
          if (carts[i].list[y].checked==true){
            zjg = zjg + carts[i].list[y].subtotal
          }
        }
      }
      this.setData({ Total: zjg})
    },
    goconfirm:function(){
      if (this.data.Total==0){
        wx.showModal({ title: '提示！', content:'你还没有选择商品'})
      }else{
        wx.navigateTo({
          url: '../confirmOrder/confirmOrder',
        })
      }
      
    }
  }
})
